# Copyright (C) 2007 "dbsolution sas" 
#
# This file is part of Weebotee.
#
# Weebotee is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Weebotee is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Weebotee.  If not, see <http://www.gnu.org/licenses/>.

from django import newforms as forms
from django.contrib.auth.models import User, Group

STATUS_CHOICES = [(0, 'Privato'), (1, 'Shared'), (2, 'Da approvare'), (3, 'Pubblico'),]
OWNER_CHOICES = [ (u.id, u.username) for u in User.objects.all() ]
#GROUP_CHOICES = [ (g.id, g.name) for g in Group.objects.all() ]

class BaseForm(forms.Form):
    status = forms.ChoiceField()
    owner = forms.ChoiceField(label='Autore')
    owner_alias = forms.CharField(label='Alias Autore', required=False)
    groupAdd = forms.MultipleChoiceField(label='Gruppi da Aggiungere',
                                         widget=forms.CheckboxSelectMultiple(),
                                         required=False)
    groupRmv = forms.MultipleChoiceField(label='Gruppi da Rimuovere', 
                                         widget=forms.CheckboxSelectMultiple(),
                                         required=False)
    
    def __init__(self, se, *args, **kwargs):
        self.base_fields['status'].choices = STATUS_CHOICES
        self.base_fields['owner'].choices = OWNER_CHOICES
        if se:
            gpAdd = [ (e.id , e.name) for e in Group.objects.exclude(id__in= [x.id for x in se.group.all()])]
            gpRemove = [(x.id, x.name) for x in se.group.all()]
        else:
            gpAdd = [ (g.id, g.name) for g in Group.objects.all() ]
            gpRemove = []
        self.base_fields['groupAdd'].choices = gpAdd
        self.base_fields['groupRmv'].choices = gpRemove
        super(BaseForm, self).__init__(*args, **kwargs)
